package com.email.crontab.framework.utils;

import cn.afterturn.easypoi.entity.vo.BigExcelConstants;
import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import cn.afterturn.easypoi.handler.inter.IExcelExportServer;
import cn.afterturn.easypoi.view.PoiBaseView;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.ModelMap;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;
 
public class FileUtil {
 
    static Logger logger = LoggerFactory.getLogger(FileUtil.class);
 
    public static final String DEFAULT_SHEET_NAME = "Sheet1";
    public static final String DEFAULT_FILE_NAME = "新建文件";
 
    public static void exportExcel(String title, String sheetName, String fileName,
                                   List<?> data, Class<?> pojoClass,
                                   HttpServletRequest request, HttpServletResponse response) {
        ModelMap modelMap = new ModelMap();
        sheetName = StringUtils.isBlank(sheetName) ? DEFAULT_SHEET_NAME : sheetName;
        fileName = StringUtils.isBlank(fileName) ? DEFAULT_FILE_NAME : fileName;
        ExportParams params = new ExportParams(title, sheetName, ExcelType.XSSF);
        // params.setFreezeCol(2);//冻结
        // 数据集合(导出的数据)
        modelMap.put(NormalExcelConstants.DATA_LIST, data);
        //导出实体
        modelMap.put(NormalExcelConstants.CLASS, pojoClass);
        //参数
        modelMap.put(NormalExcelConstants.PARAMS, params);
        //文件名称
        modelMap.put(NormalExcelConstants.FILE_NAME, fileName);
 
        PoiBaseView.render(modelMap, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
    }
 
    public static void defaultExportExcel(List<?> data, Class<?> pojoClass,
                                          HttpServletRequest request, HttpServletResponse response) {
        exportExcel(null, null, null, data, pojoClass, request, response);
    }
 
    public static <T> List<T> importExcel(MultipartFile empFile, Class<T> pojoClass, HttpServletResponse response) {
 
        ImportParams params = new ImportParams();
        params.setHeadRows(1);
        params.setNeedVerfiy(true);
        ExcelImportResult<T> result = null;
        try {
            result = ExcelImportUtil.importExcelMore(
                    empFile.getInputStream(),
                    pojoClass, params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //获取正确导入的集合
        if (result == null) {
            return null;
        }
        return result.getList();
 
    }
 
    public static void exportBigExcel(String title, String sheetName, String fileName,
                                      Class<?> pojoClass, IExcelExportServer excelExportServer,
                                      HttpServletRequest request, HttpServletResponse response) {
        sheetName = StringUtils.isBlank(sheetName) ? DEFAULT_SHEET_NAME : sheetName;
        fileName = StringUtils.isBlank(fileName) ? DEFAULT_FILE_NAME : fileName;
        ModelMap modelMap = getModelMap(title, sheetName, fileName, pojoClass, excelExportServer, null);
        PoiBaseView.render(modelMap, request, response, BigExcelConstants.EASYPOI_BIG_EXCEL_VIEW);
    }
 
    public static void exportBigExcelWithParams(String title, String sheetName, String fileName,
                                      Class<?> pojoClass, IExcelExportServer excelExportServer,Object queryParams,
                                      HttpServletRequest request, HttpServletResponse response) {
        sheetName = StringUtils.isBlank(sheetName) ? DEFAULT_SHEET_NAME : sheetName;
        fileName = StringUtils.isBlank(fileName) ? DEFAULT_FILE_NAME : fileName;
        ModelMap modelMap = getModelMap(title, sheetName, fileName, pojoClass, excelExportServer, queryParams);
        PoiBaseView.render(modelMap, request, response, BigExcelConstants.EASYPOI_BIG_EXCEL_VIEW);
    }
 
    private static ModelMap getModelMap(String title, String sheetName, String fileName, Class<?> pojoClass, IExcelExportServer excelExportServer, Object queryParams) {
        ModelMap modelMap = new ModelMap();
        ExportParams params = new ExportParams(title, sheetName, ExcelType.XSSF);
//        params.setFreezeCol(2);
        modelMap.put(BigExcelConstants.CLASS, pojoClass);
        modelMap.put(BigExcelConstants.PARAMS, params);
        //就是我们的查询参数,会带到接口中,供接口查询使用
        modelMap.put(BigExcelConstants.DATA_PARAMS, queryParams == null ? Collections.EMPTY_MAP : queryParams);
        modelMap.put(BigExcelConstants.DATA_INTER, excelExportServer);
        modelMap.put(BigExcelConstants.FILE_NAME, fileName);
        return modelMap;
    }
 
    public static void defaultExportBigExcel(String title, Class<?> pojoClass, IExcelExportServer excelExportServer,
                                             HttpServletRequest request, HttpServletResponse response) {
        exportBigExcel(title, DEFAULT_SHEET_NAME, DEFAULT_FILE_NAME, pojoClass, excelExportServer,request, response);
    }
 
    public static void defaultExportBigExcelWithParams(String title, Class<?> pojoClass, IExcelExportServer excelExportServer,Object queryParams,
                                             HttpServletRequest request, HttpServletResponse response) {
        exportBigExcelWithParams(title, DEFAULT_SHEET_NAME, DEFAULT_FILE_NAME, pojoClass, excelExportServer, queryParams,request, response);
    }

}